/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package managedbean;


import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import net.sf.jasperreports.engine.*;
import net.sf.jasperreports.engine.export.*;
import java.util.*;
import javax.persistence.EntityManager;
import entity.PaymentManagementEntity;
import entity.StakeholderEntity;
import javax.ejb.EJB;
import javax.faces.context.FacesContext;
import javax.faces.event.PhaseEvent;
import net.sf.jasperreports.engine.util.JRLoader;
import session.stateful.StakeholderSessionBean;
import session.stateful.TransactionSessionBean;


/**
 *
 * @author Dao Thai Duy
 */
@ManagedBean(name="reportManagerBean")
@RequestScoped
public class ReportManagerBean {
    private EntityManager entityManager;
    @EJB
    TransactionSessionBean transactionSessionBean;
    @EJB
    StakeholderSessionBean stakeholderSessionBean;
    StakeholderEntity selectedStakeholder;
    /** Creates a new instance of ReportManagerBean */
    public ReportManagerBean() {
    }
    
    public void initGenerateReport(PhaseEvent event) {
        selectedStakeholder = (StakeholderEntity) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("stakeholder");
        System.out.println(selectedStakeholder!=null);
    }
         
    public void testCreateBill() throws ClassNotFoundException{
//        PaymentManagementEntity previousBill; 
        PaymentManagementEntity currentBill=null;
//        previousBill=transactionSessionBean.getPaymentManagementById(Long.valueOf(3333));
//        currentBill=transactionSessionBean.getPaymentManagementById(Long.valueOf(2222));
        currentBill=transactionSessionBean.getCurrentBill(selectedStakeholder);
        System.out.println("currentBill "+currentBill.getId());
        createBill(currentBill);
    }
    public void createBill(PaymentManagementEntity currentBill) throws ClassNotFoundException {
        HashMap parameterMap = new HashMap();
        JasperReport jasperReport;
        Connection connection = establishConnection();
        
        try {
            jasperReport = JasperCompileManager.compileReport("C://IS3102Project//report//reportBill.jrxml");
//            jasperReport= (JasperReport)JRLoader.loadObject(("reportBill.jasper"));
//            parameterMap.put("previousBilling", previousBill.getTotalPaymentCurrent());
//            parameterMap.put("previousPaymentLeft", previousBill.getPaymentLeft());
//            parameterMap.put("previousPaymentReceived", previousBill.getPaymentReceived());
            parameterMap.put("currentBillingId", currentBill.getId());
            JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parameterMap, connection);
            
            JasperExportManager.exportReportToPdfFile(jasperPrint, "C://IS3102Project//IS3102Project-war//web//report//reportBill.pdf");
//            JasperExportManager.exportReportToHtmlFile(jasperPrint, "D://NetBeansProjects//IS3102Project//IS3102Project-war//web//reportBill.html" );

//            JRXlsExporter exporter = new JRXlsExporter();
//            exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
            //export report to Excel
//            exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, "D://simple_report.xls" );
//            exporter.exportReport();
        } catch (JRException e) {
            e.printStackTrace();
            System.exit(1);
        }
    }
    public static Connection establishConnection() throws ClassNotFoundException{
        Connection connection = null;
        try{
            Class.forName("com.mysql.jdbc.Driver");
            String myURL = "jdbc:mysql://localhost:3306/is3102db";
            connection = DriverManager.getConnection(myURL,"root","adminadmin");
            connection.setAutoCommit(false);
        }catch(SQLException exception){
            exception.printStackTrace();
        }
        return connection;
    }
}
